package com.heima.Listener;

import com.alibaba.fastjson.JSON;
import com.heima.Serivce.WemediamaterialChannelService;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.json.Json;

import static com.heima.common.exception.RabbitMQcode.WmChannelQueueName;

@Component
@Slf4j
public class ChannelListener {
    @Autowired
    WemediamaterialChannelService wemediamaterialChannelService;
    @RabbitListener(queues = WmChannelQueueName)
    public void Channeloperate(String data)
    {
        String type=data.split("_")[1];
        String Object=data.split("_")[0];
        if (type.equals("del"))
        {
            ChannelDelete(Object);
            log.info("Channel删除成功");
        }

        if (type.equals("save"))
        {
            ChannelSave(Object);
            log.info("Channel添加成功");
        }

        if(type.equals("update"))
        {
            ChannelUpdate(Object);
            log.info("Channel修改成功");
        }
    }

    private void ChannelDelete(String data)
    {
        Integer id= JSON.parseObject(data,Integer.class);
        wemediamaterialChannelService.DeleteChannel(id);
    }

    private void ChannelSave(String data)
    {
        WmChannel wmChannel= JSON.parseObject(data,WmChannel.class);
        wemediamaterialChannelService.SaveChannel(wmChannel);
    }

    private void ChannelUpdate(String data)
    {
        WmChannel wmChannel= JSON.parseObject(data,WmChannel.class);
        wemediamaterialChannelService.UpdateChannel(wmChannel);
    }
}
